package cn.itcast.server.service;

import java.util.concurrent.ConcurrentHashMap;

/*
*  用户信息存储类
* */
final class UserServiceMemoryImpl implements UserService {
    private final ConcurrentHashMap<String, String> allUserMap;
    private static UserServiceMemoryImpl userServiceMemory;

    private UserServiceMemoryImpl(){}

    {
        allUserMap = new ConcurrentHashMap<>();
        allUserMap.put("zhangsan", "123");
        allUserMap.put("lisi", "123");
        allUserMap.put("wangwu", "123");
        allUserMap.put("zhaoliu", "123");
        allUserMap.put("qianqi", "123");
    }

    /* 懒加载的单例模式 */
    public static UserService newInstance(){
        if(userServiceMemory == null){
            synchronized (UserServiceMemoryImpl.class){
                if(userServiceMemory == null){
                    userServiceMemory = new UserServiceMemoryImpl();
                }
            }
        }
        return userServiceMemory;
    }

    @Override
    public boolean login(String username, String password) {
        if((username == null || password == null) || (username.isEmpty() || password.isEmpty()))
            return false;

        String pass = allUserMap.get(username);
        return pass != null && pass.equals(password);
//        if (pass == null) {
//            return false;
//        }
//        return pass.equals(password);
    }
}
